package com.only4play.starfish.controller;

import com.only4play.starfish.common.RespCodeEnum;
import com.only4play.starfish.common.GenericResponse;
import com.only4play.starfish.domain.LivingArea;
import com.only4play.starfish.service.LivingAreaService;
import com.only4play.starfish.utils.PassToken;
import com.only4play.starfish.utils.UserLoginToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 房源小区控制器
 */
@RestController
@RequestMapping("/livingArea")
public class LivingAreaController {
    @Autowired
    LivingAreaService service;

    @PassToken
    @PostMapping("/areaList")
    public GenericResponse areaList() {
        return GenericResponse.success(service.areaList());
    }

    @PassToken
    @PostMapping("/getArea")
    public GenericResponse getArea(@RequestParam(value = "id", required = true) Long id) {
        return GenericResponse.success(service.getArea(id));
    }

    @UserLoginToken
    @PostMapping("/insertLivingArea")
    public GenericResponse insertLivingArea(@RequestBody LivingArea livingArea) {
        int code = service.insertLivingArea(livingArea);
        if (code == -1) {
            return GenericResponse.error(RespCodeEnum.ALREADYEXIST);
        }
        return GenericResponse.success(code);
    }

    @UserLoginToken
    @PostMapping("/deleteLivingArea")
    public GenericResponse deleteLivingArea(@RequestParam(value = "id", required = true) Long id) {
        return GenericResponse.success(service.deleteLivingArea(id));
    }

    @UserLoginToken
    @PostMapping("/updateLivingArea")
    public GenericResponse updateLivingArea(@RequestBody LivingArea livingArea) {
        return GenericResponse.success(service.updateLivingArea(livingArea));
    }
}
